package com.util.msn;

import java.util.Iterator;

import rath.msnm.BuddyGroup;
import rath.msnm.BuddyList;
import rath.msnm.MSNMessenger;
import rath.msnm.SwitchboardSession;
import rath.msnm.entity.MsnFriend;
import rath.msnm.event.MsnAdapter;
import rath.msnm.msg.MimeMessage;
/**
 * 
 * <p>功能描述:MSN消息事件处理类</p>
 * @author  <a href="mailto: zhuyoulong@gmail.com">zhuyoulong</a>
 * @create_date 2008-10-31
 * @version $ Revision: 1.0
 */
public class MSNAdapter extends MsnAdapter {
	MSNMessenger messenger;
	public MSNAdapter(MSNMessenger messenger) {
	   this.messenger = messenger;

	}
	/**
	* 某人正在输入信息
	*/
	public void progressTyping(
	   SwitchboardSession ss,
	   MsnFriend friend,
	   String typingUser) {
	   System.out.println(friend.getLoginName() + "正在输入信息...");
	}
	/**
	* 收到消息的时候执行该方法
	*/
	public void instantMessageReceived(
	   SwitchboardSession ss,
	   MsnFriend friend,
	   MimeMessage mime) {
	   System.out.print("接收到消息：" + friend.getFriendlyName() + "->");
	   System.out.println(mime.getMessage());
	   try {
	    //发送相同的回复信息给发送者
	    messenger.sendMessage(friend.getLoginName(), mime);
	   } catch (Exception e) {
	    e.printStackTrace();
	   }
	}
	/**
	 * 登录成功后执行该方法
	 */
	public void loginComplete(MsnFriend own,SwitchboardSession ss) {
	   System.out.println(own.getLoginName() + " Login OK");
	  // BuddyGroup group = messenger.getBuddyGroup();
	 //  BuddyList buddyList =group.getAllowList();
	 //  System.out.println("begin");
	 //  for (Iterator it = buddyList.iterator();it.hasNext();){
	   // MsnFriend friend = (MsnFriend)it.next();
	 //   System.out.println(friend.getFriendlyName()+":"+friend.getLoginName());
	//   }
	   //获得好友列表完直接退出msn
	   try {
		  
		   messenger.doCall("yaoyao9446@126.com");
		   Thread.sleep(15000);//最好在此有所延迟,要不SwitchboardSession创建容易失败 
			 ss = messenger.doCallWait("yaoyao9446@126.com"); 
			MimeMessage msg = new MimeMessage("你好,周末愉快!"); 
			msg.setKind(MimeMessage.KIND_MESSAGE); 
			messenger.sendMessage(ss.getMsnFriend().getLoginName(), msg);
			ss.sendInstantMessage(msg);
	   } catch (Exception e) {
	    e.printStackTrace();
	   }
	   
	   messenger.logout();
	} 
	/**
	* 登录失败后执行该方法
	*/
	public void loginError(String header) {
	   System.out.println("Login Failed: " + header);
	}
	/**
	* 好友离线时执行该方法
	*/
	public void userOffline(String loginName) {
	   System.out.println("USER " + loginName + " Logout.");
	}
	/**
	* 好友上线时执行该方法
	*/
	public void userOnline(MsnFriend friend) {
	   System.out.println("USER "+friend.getFriendlyName()+" Login.");
	}
	/**
	* 有人加我为好友时执行
	*/
	public void whoAddedMe(MsnFriend friend) {
	   System.out.println("USER " + friend.getLoginName() + " Addme.");
	   try {
	    messenger.addFriend(friend.getLoginName());
	   } catch (Exception e) {
	    e.printStackTrace();
	   }
	}
	/**
	* 有人把我从好友列表中删除时执行
	*/
	public void whoRemovedMe(MsnFriend friend) {
	System.out.println("USER "+friend.getLoginName()+" Remove me.");
	   try {
	    messenger.removeFriend(friend.getLoginName());
	   } catch (Exception e) {
	    e.printStackTrace();
	   }
	}
	
	/**
	* 写信

	*/
	public void writeToMe(MsnFriend friend) {
	System.out.println("USER "+friend.getLoginName()+" Write me.");
	   try {
		   messenger.doCall("zhu_you_long@hotmail.com");
		   Thread.sleep(1000);//最好在此有所延迟,要不SwitchboardSession创建容易失败 
			SwitchboardSession ss = messenger.doCallWait("zhu_you_long@hotmail.com"); 
			MimeMessage msg = new MimeMessage(); 
			msg.setKind(MimeMessage.KIND_MESSAGE); 
			msg.setMessage("你好,周末愉快!"); 
			ss.sendInstantMessage(msg);
	   } catch (Exception e) {
	    e.printStackTrace();
	   }
	}
	}
